package com.fz.song.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.fz.song.dao.BlogInfoMapper;
import com.fz.song.dao.BlogTypeMapper;
import com.fz.song.pojo.BlogBanner;
import com.fz.song.pojo.BlogInfo;
import com.fz.song.pojo.BlogType;
import com.fz.song.redis.JedisClient;
import com.fz.song.service.IBlogInfoService;
import com.fz.song.service.IBlogTypeService;
import com.fz.song.util.ResultObj;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BlogInfoServiceImpl implements IBlogInfoService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Autowired
    private JedisClient jedisClient;


    private static Log logger = LogFactory.getLog(BlogInfoServiceImpl.class);


    @Override
    public PageInfo<BlogInfo> getBlogInfoListData(Integer page, int limit) {
        PageHelper.startPage(page, limit);
        List<BlogInfo> listData = blogInfoMapper.getBlogInfoListData();
        PageInfo<BlogInfo> pageInfo = new PageInfo<BlogInfo>(listData);
        return pageInfo;
    }

    @Override
    public BlogInfo getBlogInfoByid(String id) {
        String key = BlogInfo.class.getName() + "#" + id;
        String value = jedisClient.get(key);
        BlogInfo blogInfo = null;
        if (value == null) {
            blogInfo = blogInfoMapper.selectByPrimaryKey(id);
            value = JSONObject.toJSONString(blogInfo);
            jedisClient.set(key, value);
        } else {
            blogInfo = JSONObject.parseObject(value, BlogInfo.class);
        }
        return blogInfo;
    }

    @Override
    public ResultObj release(BlogInfo blogInfo) {
        int i = blogInfoMapper.updateByPrimaryKeySelective(blogInfo);
        String key = BlogInfo.class.getName() + "#" + blogInfo.getId();
        String value = JSONObject.toJSONString(blogInfo);
        jedisClient.set(key, value);
        if (i == 1) {
            return ResultObj.ok("发布成功");
        }
        return ResultObj.error("发布失败");
    }

    @Override
    public ResultObj saveBlogInfo(BlogInfo blogInfo) {
        int i = blogInfoMapper.insertSelective(blogInfo);
        String key = BlogInfo.class.getName() + "#" + blogInfo.getId();
        String value = JSONObject.toJSONString(blogInfo);
        jedisClient.set(key, value);
        if (i == 1) {
            return ResultObj.ok("保存成功");
        }
        return ResultObj.error("保存失败");
    }

    @Override
    public ResultObj delBlogInfo(String id) {
        String key = BlogInfo.class.getName() + "#" + id;
        BlogInfo blogInfo = getBlogInfoByid(id);
        blogInfo.setDel(1);
        String value = JSONObject.toJSONString(blogInfo);
        jedisClient.set(key, value);
        int i = blogInfoMapper.updateByPrimaryKeySelective(blogInfo);
        if (i == 1) {
            return ResultObj.ok("删除成功");
        }
        return ResultObj.error("删除失败");
    }

    @Override
    public ResultObj updateBlogInfo(BlogInfo blogInfo) {
        String key = BlogInfo.class.getName() + "#" + blogInfo.getId();
        String value = JSONObject.toJSONString(blogInfo);
        jedisClient.set(key, value);
        int i = blogInfoMapper.updateByPrimaryKeySelective(blogInfo);
        if (i == 1) {
            return ResultObj.ok("修改成功");
        }
        return ResultObj.error("修改失败");
    }

    @Override
    public PageInfo<BlogInfo> getBlogInfoIndexListData(Integer page, int limit, Map<String, String> param) {
        PageHelper.startPage(page, limit);
        List<BlogInfo> listData = blogInfoMapper.getBlogInfoIndexListData(param);
        PageInfo<BlogInfo> pageInfo = new PageInfo<BlogInfo>(listData);
        return pageInfo;
    }

    @Override
    public Long getBlogInfoClicksByid(BlogInfo blogInfo) {
        Long val = 0L;
        if (blogInfo != null) {
            String key = BlogInfo.class.getName() + "#" + blogInfo.getId() + "#clicks";
            val = jedisClient.incr(key);
        }
        return val;
    }
}
